package com.kaiyidiannao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 大志 on 14-4-4.
 * 玩乐住列表Adaper
 */
public class PalyListAdapter extends ImageLoaderBaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<PlayBean> list = new ArrayList<PlayBean>();

    public PalyListAdapter(Context context, List<PlayBean> list) {
        this.context = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView thumeImageView = null;//缩略图
        TextView titleTextView = null;//商家名称
        TextView typeTextView = null;//商家类型
        TextView distanceTextView = null;//距离
        TextView addresstView = null;//店铺位置

        final PlayBean bean = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.play_list_item, null);
        }
        thumeImageView = (ImageView) convertView.findViewById(R.id.play_thumb);
        titleTextView = (TextView) convertView.findViewById(R.id.play_product_name);
        typeTextView = (TextView) convertView.findViewById(R.id.play_product_type);
        distanceTextView = (TextView) convertView.findViewById(R.id.play_product_distance);
        addresstView = (TextView) convertView.findViewById(R.id.play_address);

        imageLoader.displayImage(bean.getThumb(), thumeImageView, options, null);
        titleTextView.setText(bean.getTitle());
        typeTextView.setText(bean.getType());

        try {
            if (bean.getDistance() == null || bean.getDistance().equals("")) {
                distanceTextView.setVisibility(View.GONE);
            } else {
                float b = (float) (Math.round((Float.parseFloat(bean.getDistance()) / 1000) * 100)) / 100;
                distanceTextView.setText(b + "km");
            }
        } catch (Exception e) {

        }
        addresstView.setText(bean.getAddress());
        ImageButton btDelete = (ImageButton) convertView.findViewById(R.id.btDelete);
        if (DataCenter.userLogin.size() != 0 && DataCenter.userLogin != null) {
            final String userid = DataCenter.userLogin.get(0).getUserid();
            btDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String action = "delete";
                    DataCenter.position = position;
                    Log.d("FoodPalaceAdapter ", bean.getFid() + "  " + position + "");
                    ((BaseActionBarActivity) context).showProgressDialog();
                    new PostDeleteMessageInfo(bean.getFid(), userid, action).execute();
                }
            });
        }

        return convertView;
    }
}
